package com.wust.supermarket.controller;

import com.wust.supermarket.domain.Instock;
import com.wust.supermarket.domain.Merch;
import com.wust.supermarket.domain.Provider;
import com.wust.supermarket.domain.Response;
import com.wust.supermarket.service.InstockService;
import com.wust.supermarket.service.MerchService;
import com.wust.supermarket.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class InstockController {
    @Autowired
    private InstockService instockService;

    @Autowired
    private MerchService merchService;

    @Autowired
    private ProviderService providerService;

    @GetMapping("/instock/list")
    public String listInstock(Model model) {
        List<Instock> instocks = instockService.listInstocks();
        model.addAttribute("instocks", instocks);
        return "instock/listInstock";
    }

    @GetMapping("/instock/add")
    public String showInsert(Model model) {
        List<Merch> merches = merchService.listAllMerches();
        List<Provider> providers = providerService.listAllProviders();
        model.addAttribute("merches", merches);
        model.addAttribute("providers", providers);
        return "instock/addInstock";
    }

    @ResponseBody
    @GetMapping("/instock/delete/{id}")
    public Response deleteInstock(@PathVariable("id") String instockId) {
        if (instockService.deleteInstockById(instockId))
            return new Response<>(1, "删除成功", null);
        else
            return new Response<>(-1, "删除失败", null);
    }

    @ResponseBody
    @PostMapping("/instock/update")
    public Response updateInstock(@RequestBody Instock instock) {
        System.out.println(instock);
        if (instockService.updateInstock(instock))
            return new Response<>(1, "修改成功", null);
        else
            return new Response<>(-1, "修改失败", null);
    }

    @ResponseBody
    @PostMapping("/instock/add")
    public Response insertInstock(@RequestBody Instock instock) {
        System.out.println(instock);
        if (instockService.insertInstock(instock))
            return new Response<>(1, "插入成功", null);
        else
            return new Response<>(-1, "插入失败", null);
    }

}
